﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GolfLeague.EditModels;
using GolfLeague.Models;
using GolfLeague.ViewModels;

namespace GolfLeague.Controllers
{
    public class RoundController : Controller
    {
        MatchInterface matchInterface = new MatchInterface();
        RoundInterface roundInterface = new RoundInterface();
        ScoreCardInterface scoreCardInterface = new ScoreCardInterface();

        //
        // GET: /Round/

        [Authorize(Roles = "Administrator")]
        public ActionResult Index(int id)
        {
            return View(roundInterface.GetRoundEditModel(id));
        }

        //
        // POST: /Round/
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult Index(RoundsEditModel roundsEditModel)
        {
            if (ModelState.IsValid)
            {
                roundInterface.AddRound(roundsEditModel.NewRound);
                roundInterface.Save();

                foreach (int scoringMethod in roundsEditModel.ScoringMethods)
                    roundInterface.AddRoundScoringMethod(new RoundScoringMethod
                    {
                        RoundID = roundsEditModel.NewRound.RoundID,
                        ScoringMethodID = scoringMethod
                    });
                roundInterface.Save();
                
                return RedirectToAction("Index", new { id = roundsEditModel.NewRound.ScheduleID });
            }
            Round round = roundsEditModel.NewRound;
            roundsEditModel = roundInterface.GetRoundEditModel(round.ScheduleID);
            roundsEditModel.NewRound = round;
            return View(roundsEditModel);
        }

        //
        // GET: /Round/RoundEdit/5

        [Authorize(Roles = "Administrator")]
        public ActionResult RoundEdit(int id)
        {
            Round round = roundInterface.GetRound(id);
            RoundsEditModel roundsEditModel = roundInterface.GetRoundEditModel(round.ScheduleID);
            foreach (RoundScoringMethod scoringMethod in round.RoundScoringMethods)
                roundsEditModel.ScoringMethods.Add(scoringMethod.ScoringMethodID);
            roundsEditModel.NewRound = round;

            return View("Index", roundsEditModel);
        }

        //
        // POST: /Round/RoundEdit
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult RoundEdit(RoundsEditModel roundsEditModel)
        {
            if (ModelState.IsValid)
            {
                var updateRound = roundInterface.GetRound(roundsEditModel.NewRound.RoundID);
                updateRound.RoundDate = roundsEditModel.NewRound.RoundDate;
                updateRound.CourseID = roundsEditModel.NewRound.CourseID;
                updateRound.TeeBoxID = roundsEditModel.NewRound.TeeBoxID;
                updateRound.SideID = roundsEditModel.NewRound.SideID;

                if (TryUpdateModel(updateRound))
                {
                    roundInterface.Save();
                    IEnumerable<RoundScoringMethod> roundScoringMethods = roundInterface.GetRoundScoringMethods(roundsEditModel.NewRound.RoundID);
                    foreach (int scoringMethodID in roundsEditModel.ScoringMethods)
                        if (roundScoringMethods.Count(m => m.ScoringMethodID == scoringMethodID) < 1)
                            roundInterface.AddRoundScoringMethod(new RoundScoringMethod
                            {
                                RoundID = roundsEditModel.NewRound.RoundID,
                                ScoringMethodID = scoringMethodID
                            });

                    roundScoringMethods.ToList().ForEach(delegate(RoundScoringMethod roundScoringMethod)
                    {
                        if (roundsEditModel.ScoringMethods.Count(m => m == roundScoringMethod.ScoringMethodID) < 1)
                            roundInterface.DeleteRoundScoringMethod(roundScoringMethod);
                    });
                    roundInterface.Save();
                    return RedirectToAction("Index", new { id = roundsEditModel.NewRound.ScheduleID });
                }
            }
            Round round = roundsEditModel.NewRound;
            roundsEditModel = roundInterface.GetRoundEditModel(round.ScheduleID);
            roundsEditModel.NewRound = round;
            return View("Index", roundsEditModel);
        }

        //
        // GET: /Round/RoundBatch

        [Authorize(Roles = "Administrator")]
        public ActionResult RoundBatch(RoundsEditModel roundsEditModel)
        {
            DateTime nextDate = roundsEditModel.RoundBatchOptions.StartDate;
            Round round;
            List<int> RoundIDs = new List<int>();
            int frequency = roundsEditModel.RoundBatchOptions.Frequency;
            int CourseSide = roundsEditModel.RoundBatchOptions.StartingSide;

            for (int i = 0; i < roundsEditModel.RoundBatchOptions.NumberOfRounds; i++)
            {
                round = new Round
                {
                    ScheduleID = roundsEditModel.Schedule.ScheduleID,
                    CourseID = roundsEditModel.RoundBatchOptions.CourseID,
                    RoundDate = nextDate,
                    SideID = CourseSide,
                    TeeBoxID = roundsEditModel.RoundBatchOptions.TeeBoxID
                };
                roundInterface.AddRound(round);
                roundInterface.Save();
                RoundIDs.Add(round.RoundID);
                nextDate = nextDate.AddDays(frequency * 7);
                if (CourseSide == (int)CourseSides.Front)
                    CourseSide = (int)CourseSides.Back;
                else if (CourseSide == (int)CourseSides.Back)
                    CourseSide = (int)CourseSides.Front;
            }

            if (roundsEditModel.RoundBatchOptions.CreateMatches)
            {
                IEnumerable<Match> Matches = roundInterface.GenerateRoundMatches(RoundIDs, roundsEditModel.RoundBatchOptions.SelectedTeams, roundsEditModel.RoundBatchOptions.StartTime, roundsEditModel.RoundBatchOptions.MatchOffset);
                foreach (Match match in Matches) matchInterface.AddMatch(match);
                matchInterface.Save();
            }

            return RedirectToAction("Index", new { id = roundsEditModel.Schedule.ScheduleID });
        }

        //
        // POST: /Round/RoundDelete
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public ActionResult RoundDelete(int roundID)
        {
            Round round = roundInterface.GetRound(roundID);
            matchInterface.GetRoundPlayerMatches(roundID).ToList().ForEach(delegate(PlayerMatch playerMatch) { matchInterface.DeletePlayerMatch(playerMatch); });
            matchInterface.GetRoundMatches(roundID).ToList().ForEach(delegate(Match match) { matchInterface.DeleteMatch(match); });
            matchInterface.Save();
            roundInterface.GetRoundScoringMethods(roundID).ToList().ForEach(delegate(RoundScoringMethod roundScoringMethod) { roundInterface.DeleteRoundScoringMethod(roundScoringMethod); });
            roundInterface.DeleteRound(round);
            roundInterface.Save();

            return RedirectToAction("Index", new { id = round.ScheduleID });
        }

        //
        // POST: /Round/RoundTeeBoxList/5
        [HttpPost]
        [Authorize(Roles = "Administrator")]
        public JsonResult RoundTeeBoxList(int courseID)
        {
            List<StandardDropdown> teeBoxDropdown = new List<StandardDropdown>();
            foreach (TeeBox teeBox in scoreCardInterface.GetAllCourseTeeBoxes(courseID))
                teeBoxDropdown.Add(new StandardDropdown
                {
                    dropdownID = teeBox.TeeBoxID,
                    dropdownText = teeBox.TeeBoxName
                });

            return Json(new { teeBoxDropdown });
        }

    }
}
